#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include "club.h"


int main()
{
    umask(0000);
    info info, change;

    char name1[50];
    char choice[4];
    
    int option;
    
    
    

loop1:
        printf("-------------------------------------------------------------------------------------------\n");
    printf("      #############     #######       #############   ###        ##      ##         ########### \n");
    printf("     ###             ###     ###     ####     ####     ##       ####   ####        ##           \n");
    printf("   ###             ### ##### ###    ###########                ##  ####  ##       ###########  \n");
    printf(" ###             ###          ###  ####      ####             ##   ###   ##               ##   \n");
    printf("#############  ###            ### ####         ####          ##    ##    ##  #  ###########   \n");
    printf("-----------------------------------------------------------------------------------------------\n");
    printf("CAR MANAGER V1.2.5 \n");


    printf("---------------------------------欢迎进入赛车信息管理系统-----------------------------------------\n");
    printf("-----------------------------------1、进入录入模式-----------------------------------------------\n");    
    printf("-----------------------------------2、按照车型查找信息-------------------------------------------\n");
    printf("-----------------------------------3、按照车型修改信息-------------------------------------------\n");
    printf("-----------------------------------4、按照车型删除信息-------------------------------------------\n");
    printf("-----------------------------------5、退出系统--------------------------------------------------\n");
    printf("-----------------------------------6、显示所有车辆信息------------------------------------------\n");
    printf("-----------------------------------7、恢复被删除的车辆信息---------------------------------------\n");


    scanf("%d" ,&option);

switch(option)
{

    case 1:
    loop2:
        while(1)
        {
            
            printf("输入'yes'录入，输入'no'退出\n");
            
            scanf("%s", choice);
            while(getchar() != '\n' );
            if(strcmp(choice, "no") == 0 )
            {
                goto loop1;
            }
            else if(strcmp(choice, "yes") == 0 )
            {
                int fd = open("WD.txt", O_RDWR | O_CREAT   , 0777);
                if(fd == 0)
                {
                    perror("creat/open failed!!!");
                    exit(EXIT_FAILURE);
                }
                    system("clear");
            printf("输入你的圈速:单位为分/秒\n");//圈速-1>0.00 <0.60
            
                while(1)
                {
                    if(1 == scanf("%f", &info.time) ) 
                    { 
                        if(info.time > 100 || info.time < 1 || (info.time - (int)info.time) > 0.59 )
                        {
                            printf("圈速输入错误,请重新输入\n");
                            continue;
                        }
                        break;
                    }
                    else if(scanf("%f", &info.time) == 0)
                    {
                        printf("圈速输入错误,请确认清楚\n");
                        printf("请按照正确输入\n");
                        //printf("本次将退出系统\n"); 
                        //exit(1);
                        while(getchar() != '\n');
                        continue;
                    }   
               
                }
                

                printf("输入你的车型:\n");
                while(getchar() != '\n' );
                scanf("%s", info.car_no);
                

                printf("输入车辆排量:\n");
                while(getchar() != '\n' );
                scanf("%s", info.disp);
                
                
                printf("输入车辆发动机型号:\n");
                while(getchar() != '\n' );
                scanf("%s", info.engine);
                

                printf("输入车手名称:\n");
                while(getchar() != '\n' );
                scanf("%s", info.racer);  //不用合理性判断只需要判断长度
                

                
                printf("输入车辆驱动方式：\n");
                while(getchar() != '\n' );
                scanf("%s", info.d_model);
                

                info.exist = 1;

                lseek(fd, 0 , SEEK_END);
                write(fd, &info, sizeof(info));
                    
                
                close(fd);
            }
            else if(strcmp(choice, "no") != 0 || strcmp(choice, "yes") != 0 )
            {
                printf("输入错误重新输入选择\n");
                printf("按下回车后根据提示输入\n");
                while(getchar() != '\n');
                goto loop2;
            }   
        }
    break;

    case 2:
       
        system("clear");
        accord_car_no();
        
        goto loop1;
        
    break;


    case 3:
        system("clear");
        replace_info();
       
        goto loop1;
    break;

    case 4:
        system("clear");
       
        delete_info();

        goto loop1;
    break;

    case 5:
        system("clear");
        printf("感谢您的使用，谢谢\n");
        exit(1);

    break;

    case 6:
        system("clear");
       
       read_from_file();
        
        goto loop1;
    break;

    case 7:
        system("clear");
       
        recover_info();
        goto loop1;
    break;

    case 8:
        system("clear");
        struct info all[100] = {};
        struct info info;
        int fd = open("WD.txt", O_RDWR,0777);
        printf("排序结果如下\n");
        int a ; 

       while(read(fd, &info, sizeof(info)) != 0)
        {
                
                if(read(fd, &info, sizeof(info)) == 0);break;
                 
                all[a].time = info.time;
                
                strcpy(all[a].car_no ,info.car_no);
                
                strcpy(all[a].disp ,info.disp);
                
                strcpy(all[a].engine ,info.engine);

                strcpy(all[a].racer ,info.racer);

                strcpy(all[a].d_model ,info.d_model);

                all[a].exist = info.exist;

                printf("%.3f\t%s\t%s\t%s\t%s\t%s\t%d\t\n", all[a].time, all[a].car_no, all[a].disp, all[a].engine, all[a].racer, all[a].d_model, all[a].exist);
                
        }
            close(fd);
            fast_sort(all, sizeof(all)/sizeof(info));

            for(int b = 0; b < sizeof(all)/sizeof(info); b++)
            {

                printf("%.3f\t%s\t%s\t%s\t%s\t%s\t%d\t\n", all[b].time, all[b].car_no, all[b].disp, all[b].engine, all[b].racer, all[b].d_model, all[b].exist);
            }

            goto loop1;

        


    break;

    case 9:
        system("clear");
         read_all();
        goto loop1;
    break;

}
    
    
    return 0;
}